﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("PaymentMethod")]
    public partial class PaymentMethod : PageBase
    {
        private readonly PaymentMethodManage _myBLL = new PaymentMethodManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];
                if (action == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = false;
                }
                else if (action == "Edit")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = true;
                    this.btnOK.Visible = false;


                    var model = new PaymentMethodManage().GetInfo(int.Parse(Request["ID"]));
                    this.txtPaymentMethodName.Text = model.MethodName;
                    this.cbkPass.Checked = model.Pass;
                    this.txtAccountsID.Text = model.AccountsId;
                    this.txtMD5.Text = model.Md5;
                    this.txtRate.Text = model.Rate.ToString();
                    this.cbkIsDefault.Checked = model.IsDefault;
                    this.ddlInterface.SelectedValue = model.Interface.ToString();
                    this.txtParentId.Text = model.ParentId;
                    this.txteMail.Text = model.EMail;
                }
                else
                {
                    BindList();
                }
            }
        }

        private void BindList()
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);
            this.GV_List.DataSource = new PaymentMethodManage().GetList();
            this.GV_List.DataBind();

            if (this.GV_List.Rows.Count <= 0)
            {
                this.DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, EventArgs e)
        {
            this._myBLL.Delete(GetSelectedRow.GetIDs(this.GV_List), this.UpdatePanel1);
            BindList();
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = new WinnerCMS.Model.PaymentMethod
            {
                Id = 0,
                MethodName = this.txtPaymentMethodName.Text.Trim(),
                AccountsId = this.txtAccountsID.Text.Trim(),
                Md5 = this.txtMD5.Text.Trim(),
                Rate = decimal.Parse(this.txtRate.Text),
                Pass = this.cbkPass.Checked,
                IsDefault = this.cbkIsDefault.Checked,
                Admin = UserManage.Current.UserName,
                Interface = int.Parse(this.ddlInterface.SelectedValue),
                ParentId = this.txtParentId.Text.Trim(),
                EMail = this.txteMail.Text.Trim()
            };

            string Err = null;
            this._myBLL.Add(model, ref Err);

            if (string.IsNullOrEmpty(Err))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList();
            }
            else
            {
                MsgBox.Message(Err, this.UpdatePanel1);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var model = this._myBLL.GetInfo(int.Parse(Request["ID"]));

            model.MethodName = this.txtPaymentMethodName.Text;
            model.Pass = this.cbkPass.Checked;
            model.AccountsId = this.txtAccountsID.Text;
            model.Md5 = this.txtMD5.Text;
            model.Rate = decimal.Parse(this.txtRate.Text);
            model.IsDefault = this.cbkIsDefault.Checked;
            model.Admin = UserManage.Current.UserName;
            model.Interface = int.Parse(this.ddlInterface.SelectedValue);
            model.ParentId = this.txtParentId.Text.Trim();
            model.EMail = this.txteMail.Text.Trim();

            string Err = null;
            this._myBLL.Add(model, ref Err);
            if (string.IsNullOrEmpty(Err))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList();
            }
            else
            {
                MsgBox.Message(Err, this.UpdatePanel1);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("PaymentMethod.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                this._myBLL.PassOrNo((int) this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

                BindList();
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBLL.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString(), this.UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[3];
                var lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }

        private void InitSupportCoins(string interfaceId)
        {
            switch (interfaceId)
            {
                case "0":
                    //this.cblSupportCoins.Visible = false;
                    //this.ltrPaymentInfo.Text = "";
                    this.trAccountsID.Visible = false;
                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "1":

                    this.trAccountsID.Visible = false;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    this.trMD5.Visible = false;
                    break;
                case "2":
                    //this.ddlAliPaySetting.Attributes.Add("onchange", "ShowAlipayButton()");

                    this.trAccountsID.Visible = true;
                    //this.pnlAliPaySettings.Visible = true;

                    this.treMail.Visible = false;
                    this.trParentId.Visible = true;
                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>支付宝的特约商家才可以使用此支付接口，详情请联系客服</font>";
                    this.HyperLink1.NavigateUrl = "#";
                    break;

                case "3":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "4":

                    this.trAccountsID.Visible = false;

                    ////this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = true;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "5":

                    this.trAccountsID.Visible = false;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = true;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "6":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;
                    break;

                case "7":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;

                    break;

                case "8":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;
                    this.HyperLink1.Visible = false;

                    break;

                case "9":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行IPS商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://express.ips.com.cn/merchant/register_xpshop.asp";
                    break;

                case "10":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行财付通商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://union.tenpay.com/mch/mch_index1.shtml?posid=1202677201";
                    break;

                case "11":
                    //this.ddlAliPaySetting.Attributes.Add("onchange", "ShowAlipayButton()");

                    this.trAccountsID.Visible = true;
                    //this.pnlAliPaySettings.Visible = true;

                    this.treMail.Visible = false;
                    this.trParentId.Visible = true;
                    this.HyperLink1.Visible = false;

                    break;

                case "12":

                    this.trAccountsID.Visible = true;

                    //this.pnlAliPaySettings.Visible = false;
                    this.treMail.Visible = false;
                    this.trParentId.Visible = false;

                    this.HyperLink1.Visible = true;
                    this.HyperLink1.Text = "<font color=red>[点击这里进行财付通商户注册]</font>";
                    this.HyperLink1.NavigateUrl = "http://union.tenpay.com/mch/mch_index1.shtml?posid=1202677201";
                    break;
            }
        }

        protected void ddlInterface_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitSupportCoins(this.ddlInterface.SelectedValue);
            switch (this.ddlInterface.SelectedValue)
            {
                case "0":

                    this.trAccountsID.Visible = false;
                    break;

                case "1":

                    this.trAccountsID.Visible = false;
                    break;

                case "2":

                    this.trAccountsID.Visible = true;
                    break;

                case "3":

                    this.trAccountsID.Visible = true;
                    break;

                case "4":

                    this.trAccountsID.Visible = false;
                    this.treMail.Visible = true;
                    break;

                case "5":

                    this.trAccountsID.Visible = false;
                    this.treMail.Visible = true;
                    break;

                case "7":

                    this.trAccountsID.Visible = true;
                    break;

                case "8":

                    this.trAccountsID.Visible = true;
                    break;

                case "9":

                    this.trAccountsID.Visible = true;
                    this.trParentId.Visible = false;
                    break;

                case "10":

                    this.trAccountsID.Visible = true;
                    break;

                case "11":

                    this.trAccountsID.Visible = true;
                    break;

                case "12":

                    this.trAccountsID.Visible = true;
                    break;
            }
        }

        protected string PayPlat(object obj)
        {
            switch (obj.ToString())
            {
                case "1":
                    return "线下支付";
                case "2":
                    return "支付宝支付接口";
                case "3":
                    return "网银支付接口(国内)";
                case "4":
                    return "Paypal";
                case "5":
                    return "贝宝";
                case "6":
                    return "网银支付接口(国外)";
                case "7":
                    return "快钱支付";
                case "8":
                    return "YeePay易宝支付";
                case "9":
                    return "IPS易捷支付";

                case "10":
                    return "腾讯财付通即时到帐支付";
                case "11":
                    return "支付宝鲜花支付接口";
                case "12":
                    return "腾讯财付通中介担保支付";
            }
            return "";
        }
    }
}